package net.net23.sysdent.client.ui;

import java.util.Map;

import net.net23.sysdent.client.services.model.MapService;
import net.net23.sysdent.client.services.model.MapServiceAsync;
import net.net23.sysdent.client.ui.widgets.model.ContentWidget;
import net.net23.sysdent.client.ui.widgets.model.T4LLayoutManager;
import net.net23.sysdent.shared.MapServiceContants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;

public class T4LLayout extends Composite implements T4LLayoutManager {

	private DockPanel dock;

	private T4LHeader t4lHeader;
	private T4LHorinzontalMenu t4lMenu;
	private T4LContent t4lContent;

	private MapServiceAsync mapService = GWT.create(MapService.class);

	public T4LLayout() {
		dock = new DockPanel();
		dock.ensureDebugId("t4l-layout");
		dock.setStyleName("t4l-layout");
		initT4LHeader();
		initT4LContent();
		initT4LMenu();
		initWidget(dock);
	}

	private void initT4LHeader() {
		t4lHeader = new T4LHeader();
		dock.add(t4lHeader, DockPanel.NORTH);
	}

	private void initT4LMenu() {
		t4lMenu = new T4LHorinzontalMenu();
		t4lMenu.setLayoutManager(this);
		dock.add(t4lMenu, DockPanel.NORTH);
	}

	private void initT4LContent() {
		t4lContent = new T4LContent();
		dock.add(t4lContent, DockPanel.CENTER);
	}

	@Override
	protected void onLoad() {
		mapService.execute(MapServiceContants.GET_USER_INFO, null,
				new AsyncCallback<Map<Object, Object>>() {
					@Override
					public void onSuccess(Map<Object, Object> result) {
						t4lHeader.setUserInfo(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}
				});
	}

	@Override
	public void setContents(ContentWidget content) {
		t4lContent.add(content);
	}

}
